﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SignalShopBase.Collections;
using SignalShopBase;
using System.Reflection;
using System.IO;
using SignalShopBase.Collections.DictionaryTree;

namespace SignalShop
{
    public class ModuleRepository
    {
        private DictionaryTreeNode<SignalShopModule> modules;
        public DictionaryTreeNode<SignalShopModule> Modules { get { return modules; } }

        public ModuleRepository()
        {
            modules = new DictionaryTreeNode<SignalShopModule>();
        }

        public void Load(string[] files)
        {
            foreach (var file in files)
            {
                var assembly = Assembly.LoadFile(file);

                foreach (var type in assembly.GetTypes())
                {
                    if (type.IsSubclassOf(typeof(SignalShopModule)))
                    {
                        var module = (SignalShopModule)Activator.CreateInstance(type);

                        modules.Add(module.Path, module);

                        if (ModuleLoaded != null)
                            ModuleLoaded(this, new ModuleRepositoryModuleLoadedEventArgs(module));
                    }
                }
            }
        }

        #region Events

        public event ModuleRepositoryModuleLoadedEventHandler ModuleLoaded;

        #endregion
    }
}
